﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace YemekteyizLib.Bs
{
    public class UyeAdresProvider
    {
        public static void SilAdres(UyeAdres u)
        {
            SqlCommand cmd = new SqlCommand("delete from Adresler where AdresID = @ID");
            cmd.Parameters.AddWithValue("@ID", u.ID);

            cmd.Connection = Tools.GetConnection();
            cmd.ExecuteNonQuery();
            Tools.CloseConnection();
        }

        public static void Kaydet(UyeAdres u)
        {
            SqlCommand cmd = new SqlCommand("update Adresler set IlceID=@p1, Adres=@p2, Tarif = @p3, Telefon = @p4 where AdresID = @p5");
            cmd.Parameters.AddWithValue("@p1", u.Ilcesi.ID);
            cmd.Parameters.AddWithValue("@p2", u.Adresi);
            cmd.Parameters.AddWithValue("@p3", u.Tarifi);
            cmd.Parameters.AddWithValue("@p4", u.Telefonu);
            cmd.Parameters.AddWithValue("@p5", u.ID);

            cmd.Connection = Tools.GetConnection();
            cmd.ExecuteNonQuery();
            Tools.CloseConnection();

        }


        public static void EkleAdres(UyeAdres u)
        {
            SqlCommand cmd = new SqlCommand("insert into Adresler values(@p1,@p2,@p3,@p4,@p5)");
            cmd.Parameters.AddWithValue("@p1", u.Sahibi.ID);
            cmd.Parameters.AddWithValue("@p2", u.Ilcesi.ID);
            cmd.Parameters.AddWithValue("@p3", u.Adresi);
            cmd.Parameters.AddWithValue("@p4", u.Tarifi);
            cmd.Parameters.AddWithValue("@p5", u.Telefonu);

            cmd.Connection = Tools.GetConnection();
            cmd.ExecuteNonQuery();
            Tools.CloseConnection();
        }

        public static List<UyeAdres> GetAdreslerByUye(Uye u)
        {
            List<UyeAdres> donecek = new List<UyeAdres>();
            List<string> idler = new List<string>();

            SqlCommand cmd = new SqlCommand("select * from Adresler where UyeID=@ID");
            cmd.Parameters.AddWithValue("@ID", u.ID);

            cmd.Connection = Tools.GetConnection();

            SqlDataReader rdr = cmd.ExecuteReader();


            while (rdr.Read())
            {
                UyeAdres adres = new UyeAdres { ID = rdr.GetInt32(0), Tarifi = rdr.GetString(4), Adresi = rdr.GetString(3), Telefonu = rdr.GetString(5) };
                donecek.Add(adres);

                idler.Add(rdr.GetInt32(1).ToString() + ";" + rdr.GetInt32(2).ToString());
            }

            for (int i = 0; i < donecek.Count; i++)
            {
                UyeAdres adresim = donecek[i];
                adresim.Sahibi = UyeProvider.GetByID(Convert.ToInt32(idler[i].Split(';')[0]));
                adresim.Ilcesi = IlceProvider.GetByID(Convert.ToInt32(idler[i].Split(';')[1]));
            }

            return donecek;
        }

        public static UyeAdres GetByID(int id)
        {
            int ilceID = 0;
            int uyeID = 0;

            SqlCommand cmd = new SqlCommand("select * from Adresler where AdresID = @ID");
            cmd.Parameters.AddWithValue("@ID", id);

            cmd.Connection = Tools.GetConnection();
            SqlDataReader rdr = cmd.ExecuteReader();

            rdr.Read();

            UyeAdres adres = new UyeAdres { ID = rdr.GetInt32(0), Tarifi = rdr.GetString(4), Adresi = rdr.GetString(3), Telefonu = rdr.GetString(5) };
            ilceID = rdr.GetInt32(2);
            uyeID = rdr.GetInt32(1);

            Tools.CloseConnection();

            adres.Ilcesi = IlceProvider.GetByID(ilceID);
            adres.Sahibi = UyeProvider.GetByID(uyeID);

            return adres;
            

        }
    }
}
